package com.dlc.demo.daiqianma.model;

import android.util.Log;
import com.dlc.demo.daiqianma.entity.UserEntity;
import com.dlc.demo.daiqianma.rservice.UserService;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * 用户相关数据请求
 */
public class UserModel {
    /**
     * 用户登录
     * @param userName 用户名
     * @param pwd 密码
     * @param callback 回调
     */
    public static  void doLogin(String userName,String pwd,final Callback<UserEntity> callback){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(API.BASE_URL)//基础URL
                .addConverterFactory(GsonConverterFactory.create())//设置 Json 转换器
                .build();
        UserService userService = retrofit.create(UserService .class);
        Call<UserEntity> call = userService.doLogin(userName,pwd);
        call.enqueue(new retrofit2.Callback<UserEntity>() {
            @Override
            public void onResponse(Call<UserEntity> call, Response<UserEntity> response) {
                //测试数据返回
                UserEntity user = response.body();
                callback.onSuccess(user);
                callback.onComplete();
            }

            @Override
            public void onFailure(Call<UserEntity> call, Throwable t) {
                Log.e("TAG", "Throwable : " + t);
                callback.onFailure(t.toString());
                callback.onComplete();
            }
        });
    }
}
